package zookeeperJavaAPI;
import org.apache.zookeeper.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;


/**
 * watcher 只能注册一次
 * 不支持断线重连
 * API繁琐*/
public class ZKoperate implements Watcher
{

    public static final String SERVER_PATH = "101.201.150.42:2181";
    public static final Integer TIME_OUT = 5000;

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ZooKeeper zooKeeper = new ZooKeeper(SERVER_PATH, TIME_OUT, new ZKConnect());
        System.out.println(zooKeeper.getState());
        Thread.sleep(2000);
        System.out.println(zooKeeper.getState());
        /**
         * path:创建目录
         * data：存储数据
         * acl权限
         * mode：永久，临时，顺序*/
        zooKeeper.create("/test5", "test".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zooKeeper.getState();
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        System.out.println(watchedEvent);
    }
}
